using System;
//using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Xml;

namespace Armory
{
	struct MeleeWeaponStats
	{
		private string itemId;
		private string weaponSkill;
		private string weaponSkillRating;
		private string dps;
		private string rangeMax;
		private string rangeMin;
		private string speed;

		public string ItemId { get { return itemId; } set { itemId = value; } }
		public string WeaponSkill { get { return weaponSkill; } set { weaponSkill = value; } }
		public string WeaponSkillRating { get { return weaponSkillRating; } set { weaponSkillRating = value; } }
		public string Dps { get { return dps; } set { dps = value; } }
		public string RangeMax { get { return rangeMax; } set { rangeMax = value; } }
		public string RangeMin { get { return rangeMin; } set { rangeMin = value; } }
		public string Speed { get { return speed; } set { speed = value; } }
	}
	struct MeleeStats
	{
		public MeleeWeaponStats MainHandWeapon;
		public MeleeWeaponStats OffHandWeapon;

		private string hasteRating;
		private string attackPower;
		private string hitRating;
		private string critChance;

		public string HasteRating { get { return hasteRating; } set { hasteRating = value; } }
		public string AttackPower { get { return attackPower; } set { attackPower = value; } }
		public string HitRating { get { return hitRating; } set { hitRating = value; } }
		public string CritChance { get { return critChance; } set { critChance = value; } }
	}

	class Character
	{
		private int maxEnergy = 100;
		public int MaxEnergy { get { return maxEnergy; } set { maxEnergy = value; } }
		private int maxMana;
		public int MaxMana { get { return maxMana; } set { maxMana = value; } }
		private int maxRage;
		public int MaxRage { get { return maxRage; } set { maxRage = value; } }
		
		private int currentEnergy = 100;
		public int CurrentEnergy { get { return currentEnergy; } set { currentEnergy = value; } }
		private int currentMana;
		public int CurrentMana { get { return currentMana; } set { currentMana = value; } }
		private int currentRage;
		public int CurrentRage { get { return currentRage; } set { currentRage = value; } }

		public MeleeStats MeleeStats;

		public Character(string serverName, string characterName)
		{ 
			this.populateCharacter(serverName, characterName);
		}

		private string name;

		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		private XmlDocument getXMLFeed(string URL)
		{
			HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(URL);
			rssFeed.UserAgent = "Firefox/2.0";

			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(rssFeed.GetResponse().GetResponseStream());

			return xmlDoc;
		}

		private void populateCharacter(string serverName, string characterName)
		{
			XmlDocument characterXMLFeed = this.getXMLFeed("http://armory.worldofwarcraft.com/character-sheet.xml?r=" + serverName + "&n=" + characterName);

			XmlNodeList itemsNodeList = characterXMLFeed.SelectNodes("/page/characterInfo/characterTab/items");

			foreach(XmlNode itemNode in itemsNodeList)
			{
				foreach(XmlNode statNode in itemNode.ChildNodes)
				{
					//slot 15 // slot 16
					if(statNode.Attributes["slot"].Value == "15")
					{
						this.MeleeStats.MainHandWeapon.ItemId = statNode.Attributes["id"].Value;
					}
					if(statNode.Attributes["slot"].Value == "16")
					{
						this.MeleeStats.OffHandWeapon.ItemId = statNode.Attributes["id"].Value;
					}
				}
			}

			XmlNodeList meleeNodeList = characterXMLFeed.SelectNodes("/page/characterInfo/characterTab/melee");

			foreach(XmlNode meleeitemNode in meleeNodeList)
			{
				foreach(XmlNode statNode in meleeitemNode.ChildNodes)
				{
					if(statNode.Name == "mainHandWeaponSkill")
					{
						this.MeleeStats.MainHandWeapon.WeaponSkill = statNode.Attributes["rating"].Value;
						this.MeleeStats.MainHandWeapon.WeaponSkillRating = statNode.Attributes["value"].Value;
					}
					if(statNode.Name == "offHandWeaponSkill")
					{
						this.MeleeStats.OffHandWeapon.WeaponSkill = statNode.Attributes["rating"].Value;
						this.MeleeStats.OffHandWeapon.WeaponSkillRating = statNode.Attributes["value"].Value;
					}
					if(statNode.Name == "mainHandDamage")
					{
						this.MeleeStats.MainHandWeapon.RangeMin = statNode.Attributes["min"].Value;
						this.MeleeStats.MainHandWeapon.RangeMax = statNode.Attributes["max"].Value;
						this.MeleeStats.MainHandWeapon.Speed = statNode.Attributes["speed"].Value;
					}

					if(statNode.Name == "offHandDamage")
					{
						this.MeleeStats.OffHandWeapon.RangeMin = statNode.Attributes["min"].Value;
						this.MeleeStats.OffHandWeapon.RangeMax = statNode.Attributes["max"].Value;
						this.MeleeStats.OffHandWeapon.Speed = statNode.Attributes["speed"].Value;
					}

					if(statNode.Name == "power")
					{
						this.MeleeStats.AttackPower = statNode.Attributes["effective"].Value;
					}

					if(statNode.Name == "hitRating")
					{
						this.MeleeStats.HitRating = statNode.Attributes["increasedHitPercent"].Value;
					}

					if(statNode.Name == "critChance")
					{
						this.MeleeStats.CritChance = statNode.Attributes["percent"].Value;
					}
				}
			}

			XmlDocument talentXMLFeed = this.getXMLFeed("http://armory.worldofwarcraft.com/character-talents.xml?r=" + serverName + "&n=" + characterName);
		}
	}
}
